<?php

namespace backend\modules\mbapi\controllers;

use backend\modules\mbapi\base\RestApiBaseController;
use backend\modules\mbapi\models\LoginForm;
use Yii;
use yii\web\IdentityInterface;

/**
 * Default controller for the `mbapi` module
 */
class DefaultController extends RestApiBaseController
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        return ['code' => 0];
    }

    /**
     * @return mixed
     */
    public function actionLogin()
    {
        $model = new LoginForm;
        // $model->setAttributes(Yii::$app->request->post());
        $model->setAttributes(['username' => 'admin', 'password' => '123456']);
        if ($user = $model->login()) {
            if ($user instanceof IdentityInterface) {
                return $user->access_token;
            } else {
                return $user->errors;
            }
        } else {
            return $model->errors;
        }

    }
}
